package com.qtt.app.controller;

import com.qtt.app.common.ApiResponse;
import com.qtt.app.entity.OrderForm;
import com.qtt.app.entity.RequestData.AddOrderAndReg;
import com.qtt.app.service.OrderFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理", description = "OrderController")   //tags是api名字，description是简介，value:已废用
@RestController
@RequestMapping("/Order")
public class OrderFormController {
    @Autowired
    OrderFormService orderFormService;

    @ApiOperation(value = "查看所有订单", notes = "order表中的订单id并非简单的1、2、3之类的")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @GetMapping("/list")
    public ApiResponse<List<OrderForm>> listall(){
        orderFormService.listAll();
        List<OrderForm> entities = orderFormService.listAll();
        return ApiResponse.<List<OrderForm>>builder().code(200).message("查看成功").data(entities).build();
    }


    @ApiOperation(value = "查看某个用户的订单（0全部、1未支付、2已支付、3退款）", notes = "")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @GetMapping("/OrderStateList/{userId}")
    public ApiResponse<List<OrderForm>> listByIdAndState(@ApiParam(value="用户id",required = true) @PathVariable(value="userId",required=true) Long userId,
                                                         @ApiParam(value="订单状态,0全部、1未支付、2已支付、3退款",required = true) @RequestParam(value="orderState",required=true) Integer orderState){
        List<OrderForm> entities = orderFormService.getOrderByIdAndState(userId,orderState);
        if (entities.size() ==0) {//注意列表如果返回的是空，需要这么判断，空列表为[]
            return ApiResponse.<List<OrderForm>>builder().code(400).message("还没有此类订单").data(entities).build();
        }
        return ApiResponse.<List<OrderForm>>builder().code(200).message("查看成功").data(entities).build();
    }

    @ApiOperation(value = "订单付款", notes = "")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @PostMapping("/payOrder")
    public ApiResponse<String> payOrder(@ApiParam(value="请求参数：flag输入1，表示已下单但还未付钱，输入2表示已经付好钱",required = true) @RequestBody(required = true) AddOrderAndReg requestData){

        int payFlag = orderFormService.payOrder(requestData.getUserId(),requestData.getGeneralId(),requestData.getRealName(),requestData.getPhone(),requestData.getRealPrice(),requestData.getSumPrice(),requestData.getPeopleNum(),requestData.getFlag());
        if (payFlag ==0) {
            return ApiResponse.<String>builder().code(400).message("添加订单失败").data(null).build();
        }
        return ApiResponse.<String>builder().code(200).message("添加订单成功").data(null).build();
    }



}
